/*
 * @lc app=leetcode.cn id=40 lang=cpp
 *
 * [40] 组合总和 II
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>

using namespace std;
// @lc code=start
class Solution {
public:
    vector<int> tmp;
    vector<vector<int>> ret;
    void dfs(vector<int>& candidates, int target,int pos,int sum) {
        if(sum > target) {
            return ;
        }
        if(sum == target) {
            ret.push_back(tmp);
            return ;
        }
        for(int i = pos;i < candidates.size();i++) {
            //去除重复 #i > pos保留了第一条最全的路径
            if(i > pos && candidates[i] == candidates[i-1]) {
                continue;
            }
            tmp.push_back(candidates[i]);
            sum += candidates[i];
            dfs(candidates,target,i+1,sum);
            sum -= candidates[i];
            tmp.pop_back();
        }
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        dfs(candidates,target,0,0);
        return ret;
    }
};
// @lc code=end

